package com.example.myactspringboot;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Create 2023-10-09 21:29
 * @Description
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyActSpringBootApplication.class)
public class multiInstance {
/*    @Autowired
    private IActReModelService actReModelService;*/

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("processes/multiInstance.bpmn20.xml") // 加载流程资源文件
                .name("multiInstance流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**582538
     * 启动流程实例  act_re_procdef
     */
    @Test
    public void start() {
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("multiInstance"); // 流程定义表的KEY字段值
        ProcessInstance pi = runtimeService.startProcessInstanceById("process:2:582541"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }
//327501
    /**流程实例ID:555001
     流程定义ID:multiInstance-reject:17:357504
     * 查看任务
     */
    @Test
    public void queryTask() {
        List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask("572505");
        Task task1 = taskService.createTaskQuery().taskId("572505").singleResult();
//        taskService.addComment(task1.getId(),task1.getProcessInstanceId(),"试试加备注");
        List<Comment> taskComments = taskService.getTaskComments("572505");
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        List<HistoricActivityInstance> historicActivityInstanceList = historicActivityInstanceQuery.processInstanceId(task1.getProcessInstanceId()).list();
        for (HistoricActivityInstance historicActivityInstance : historicActivityInstanceList) {
            List<Comment> taskComments1 = taskService.getTaskComments(historicActivityInstance.getTaskId(),"comment");
            if (taskComments1.size()>0)
            System.out.println(taskComments1.get(0).getFullMessage());
        }
//        taskService.getTaskComments();
        List<Task> list = taskService.createTaskQuery().taskCandidateUser("1").list();
        List<Task> list1 = taskService.createTaskQuery().taskAssignee("1").list();
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("admin") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    //根据条件查看待办
    @Test
    public void daiban(){
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> list = taskQuery.taskCandidateUser("1001").list();
        for (Task task : list) {
            System.out.println(task);
            taskService.claim(task.getId(),"1001");
        }
    }
    /**
     * 完成任务1
     */
    @Test
    public void completeTask2() {
        Map<String, Object> variables = new HashMap<String, Object>();
//        taskService.claim("327505","1");
//        taskService.complete("327505");
        variables.put("users", Arrays.asList("test1,test2,test3,test4".split(",")));
        taskService.complete("360005", variables);
    }

    /**
     * 完成任务2
     */
    @Test
    public void completeTask() {
//        taskService.complete("187523");
  /*      List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("admin") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }*/

        //.activityType("userTask")   userTask 是act_hi_actinst表里的字段
        HistoricActivityInstanceQuery hisquery = historyService.createHistoricActivityInstanceQuery();
        //.finished()  条件已完成
        List<HistoricActivityInstance> hislist = hisquery.processInstanceId("290001")
                .finished().list();
        for (HistoricActivityInstance historicActivityInstance : hislist) {
            System.out.println(historicActivityInstance);
        }

    }

    /**
     * 完成任务2
     * 跟前一个不同的地方在于完成条件，${nrOfCompletedInstances/nrOfInstances >= 0.5 || flag==false}
     * 如果flag变量为false 就直接结束当前节点
     */
    @Test
    public void completeTask3() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("flag", false);  //一票否决
        taskService.complete("55039", variables);
    }



}
